package com.jandn.media.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import com.jandn.common.action.BaseAction;

public class ChatAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	private ChatDTO dto;
	
	public boolean chackSession(){
		String login = (String)getSession().get("userId");
		if(login == null || "".equals(login)){
			return false;
		}
		return true;
	}
	
	public String insert(){
		if(!chackSession()){
			return null;
		}
		ChatUtil.insertIntoCache(dto);
		return null;
	}
	
	public String select() throws IOException{
		if(!chackSession()){
			return null;
		}
		String userid = (String)getSession().get("userId");
		int i = (Integer)getSession().get("uuid");
		List<ChatDTO> list = ChatUtil.selectFormCache(userid, i);
		if(list!=null && list.size()!=0){
			getSession().put("uuid",list.get(list.size()-1).getUuid());
		}
		try {
			JSONArray jList = (JSONArray)Json.getJson(list);
			getResponse().setContentType("text/html");
			getResponse().setCharacterEncoding("UTF-8");
			PrintWriter out = getResponse().getWriter();
			out.println(jList.toString());
			out.flush();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	public ChatDTO getDto() {
		return dto;
	}

	public void setDto(ChatDTO dto) {
		this.dto = dto;
	}
}
